/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package carstoredb.models;

import carstoredb.models.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author wuhao
 */
public class UserInfoJpaController implements Serializable {

	public UserInfoJpaController(EntityManagerFactory emf) {
		this.emf = emf;
	}
	private EntityManagerFactory emf = null;

	public EntityManager getEntityManager() {
		return emf.createEntityManager();
	}

	public void create(UserInfo userInfo) {
		EntityManager em = null;
		try {
			em = getEntityManager();
			em.getTransaction().begin();
			User userId = userInfo.getUserId();
			if (userId != null) {
				userId = em.getReference(userId.getClass(), userId.getUserId());
				userInfo.setUserId(userId);
			}
			Car carID = userInfo.getCarID();
			if (carID != null) {
				carID = em.getReference(carID.getClass(), carID.getId());
				userInfo.setCarID(carID);
			}
			em.persist(userInfo);
			if (userId != null) {
				userId.getUserInfoCollection().add(userInfo);
				userId = em.merge(userId);
			}
			if (carID != null) {
				carID.getUserInfoCollection().add(userInfo);
				carID = em.merge(carID);
			}
			em.getTransaction().commit();
		} finally {
			if (em != null) {
				em.close();
			}
		}
	}

	public void edit(UserInfo userInfo) throws NonexistentEntityException, Exception {
		EntityManager em = null;
		try {
			em = getEntityManager();
			em.getTransaction().begin();
			UserInfo persistentUserInfo = em.find(UserInfo.class, userInfo.getId());
			User userIdOld = persistentUserInfo.getUserId();
			User userIdNew = userInfo.getUserId();
			Car carIDOld = persistentUserInfo.getCarID();
			Car carIDNew = userInfo.getCarID();
			if (userIdNew != null) {
				userIdNew = em.getReference(userIdNew.getClass(), userIdNew.getUserId());
				userInfo.setUserId(userIdNew);
			}
			if (carIDNew != null) {
				carIDNew = em.getReference(carIDNew.getClass(), carIDNew.getId());
				userInfo.setCarID(carIDNew);
			}
			userInfo = em.merge(userInfo);
			if (userIdOld != null && !userIdOld.equals(userIdNew)) {
				userIdOld.getUserInfoCollection().remove(userInfo);
				userIdOld = em.merge(userIdOld);
			}
			if (userIdNew != null && !userIdNew.equals(userIdOld)) {
				userIdNew.getUserInfoCollection().add(userInfo);
				userIdNew = em.merge(userIdNew);
			}
			if (carIDOld != null && !carIDOld.equals(carIDNew)) {
				carIDOld.getUserInfoCollection().remove(userInfo);
				carIDOld = em.merge(carIDOld);
			}
			if (carIDNew != null && !carIDNew.equals(carIDOld)) {
				carIDNew.getUserInfoCollection().add(userInfo);
				carIDNew = em.merge(carIDNew);
			}
			em.getTransaction().commit();
		} catch (Exception ex) {
			String msg = ex.getLocalizedMessage();
			if (msg == null || msg.length() == 0) {
				Integer id = userInfo.getId();
				if (findUserInfo(id) == null) {
					throw new NonexistentEntityException("The userInfo with id " + id + " no longer exists.");
				}
			}
			throw ex;
		} finally {
			if (em != null) {
				em.close();
			}
		}
	}

	public void destroy(Integer id) throws NonexistentEntityException {
		EntityManager em = null;
		try {
			em = getEntityManager();
			em.getTransaction().begin();
			UserInfo userInfo;
			try {
				userInfo = em.getReference(UserInfo.class, id);
				userInfo.getId();
			} catch (EntityNotFoundException enfe) {
				throw new NonexistentEntityException("The userInfo with id " + id + " no longer exists.", enfe);
			}
			User userId = userInfo.getUserId();
			if (userId != null) {
				userId.getUserInfoCollection().remove(userInfo);
				userId = em.merge(userId);
			}
			Car carID = userInfo.getCarID();
			if (carID != null) {
				carID.getUserInfoCollection().remove(userInfo);
				carID = em.merge(carID);
			}
			em.remove(userInfo);
			em.getTransaction().commit();
		} finally {
			if (em != null) {
				em.close();
			}
		}
	}

	public List<UserInfo> findUserInfoEntities() {
		return findUserInfoEntities(true, -1, -1);
	}

	public List<UserInfo> findUserInfoEntities(int maxResults, int firstResult) {
		return findUserInfoEntities(false, maxResults, firstResult);
	}

	private List<UserInfo> findUserInfoEntities(boolean all, int maxResults, int firstResult) {
		EntityManager em = getEntityManager();
		try {
			CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
			cq.select(cq.from(UserInfo.class));
			Query q = em.createQuery(cq);
			if (!all) {
				q.setMaxResults(maxResults);
				q.setFirstResult(firstResult);
			}
			return q.getResultList();
		} finally {
			em.close();
		}
	}

	public UserInfo findUserInfo(Integer id) {
		EntityManager em = getEntityManager();
		try {
			return em.find(UserInfo.class, id);
		} finally {
			em.close();
		}
	}

	public int getUserInfoCount() {
		EntityManager em = getEntityManager();
		try {
			CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
			Root<UserInfo> rt = cq.from(UserInfo.class);
			cq.select(em.getCriteriaBuilder().count(rt));
			Query q = em.createQuery(cq);
			return ((Long) q.getSingleResult()).intValue();
		} finally {
			em.close();
		}
	}
	
}
